// stores/networkStore.ts - Qܶ�
import { create } from 'zustand';
import { networkService } from '../services/networks';
import type { NetworkDetail, NetworkSummary, ApiResponse } from '../types/api';

interface NetworkStore {
  networks: NetworkSummary[];
  currentNetwork: NetworkDetail | null;
  loading: boolean;
  error: string | null;

  fetchNetworks: (connection: string) => Promise<void>;
  getNetworkDetail: (connection: string, name: string) => Promise<NetworkDetail>;
  createNetwork: (connection: string, xmlConfig: string) => Promise<ApiResponse>;
  deleteNetwork: (connection: string, name: string) => Promise<ApiResponse>;
  startNetwork: (connection: string, name: string) => Promise<ApiResponse>;
  stopNetwork: (connection: string, name: string) => Promise<ApiResponse>;
  clearError: () => void;
}

export const useNetworkStore = create<NetworkStore>((set, get) => ({
  networks: [],
  currentNetwork: null,
  loading: false,
  error: null,

  fetchNetworks: async (connection: string) => {
    set({ loading: true, error: null });
    try {
      const response = await networkService.listNetworks(connection);
      set({
        networks: response.networks || response.items || [],
        loading: false
      });
    } catch (error) {
      set({
        error: 'Failed to fetch networks',
        loading: false
      });
    }
  },

  getNetworkDetail: async (connection: string, name: string) => {
    set({ loading: true, error: null });
    try {
      const network = await networkService.getNetworkDetail(connection, name);
      set({
        currentNetwork: network,
        loading: false
      });
      return network;
    } catch (error) {
      set({
        error: 'Failed to fetch network details',
        loading: false
      });
      throw error;
    }
  },

  createNetwork: async (connection: string, xmlConfig: string) => {
    set({ loading: true, error: null });
    try {
      const response = await networkService.createNetwork(connection, xmlConfig);
      await get().fetchNetworks(connection);
      set({ loading: false });
      return response;
    } catch (error) {
      set({
        error: 'Failed to create network',
        loading: false
      });
      throw error;
    }
  },

  deleteNetwork: async (connection: string, name: string) => {
    set({ loading: true, error: null });
    try {
      const response = await networkService.deleteNetwork(connection, name);
      await get().fetchNetworks(connection);
      set({ loading: false });
      return response;
    } catch (error) {
      set({
        error: 'Failed to delete network',
        loading: false
      });
      throw error;
    }
  },

  startNetwork: async (connection: string, name: string) => {
    try {
      const response = await networkService.startNetwork(connection, name);
      await get().fetchNetworks(connection);
      return response;
    } catch (error) {
      set({ error: 'Failed to start network' });
      throw error;
    }
  },

  stopNetwork: async (connection: string, name: string) => {
    try {
      const response = await networkService.stopNetwork(connection, name);
      await get().fetchNetworks(connection);
      return response;
    } catch (error) {
      set({ error: 'Failed to stop network' });
      throw error;
    }
  },

  clearError: () => set({ error: null }),
}));